
void setup(void){ 

  

  
  pinMode(A_SW, INPUT);   // slower/ faster tap tempo setup
  pinMode(B_SW, INPUT);
  pinMode(tapLED, OUTPUT);  /* tap tempo led */
  pinMode(wave_led_pin, OUTPUT); /* wave table tempo led */
  
  Serial.begin(9600);     // for debug to computer
  Serial2.begin(19200);    // 9600 baud is LCD chip comm speed
  Serial3.begin(31250);     //MIDI

//*************memory test************************
//    int result = memoryTest();
//  Serial.print("Memory test results: ");
//  Serial.print(result,DEC);
//  Serial.print(" bytes free");


delay (2000); // wait for LCD to start
 
//******************setup SPI pins and initialize pots****************************

  byte clr = 0;
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECTA,OUTPUT);
  pinMode(SLAVESELECTB,OUTPUT);
  digitalWrite(SLAVESELECTA,HIGH); //disable device
  digitalWrite(SLAVESELECTB,HIGH); //disable device
  
  // SPCR = 01010000
  //interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
  //sample on leading edge of clk,system clock/4 (fastest)
  SPCR = (1<<SPE)|(1<<MSTR);
  clr=SPSR;
  clr=SPDR;
  delay(10);
 
  for (int i=0; i<6; i++)
  {
    write_pot_a(i,125);  //"zero" everything          ***** set this at mid-level, not zero??? problem with latching up sometimes
    write_pot_b(i,125);
  }




////*******************default setup the 4-state machine function pointers and routings********************


  functArr[0] = &LEFT_PED;              //seems to want to have these pointers in setup... can't get the Set_And_Restore function to work here... 
  functArr[1] = &LEFT_PED;
  functArr[2] = &RIGHT_PED;
  functArr[3] = &RIGHT_PED;
//
//  functArr[0] = &LEFT_PED;
//  functArr[1] = &LEFT_PED_INV;
//  functArr[2] = &RIGHT_PED;
//  functArr[3] = &RIGHT_PED_INV;
//
//  PREV_functArr[0] = &LEFT_PED;        //setup for default previous functions, will change this when user interface is designed
//  PREV_functArr[1] = &LEFT_PED_INV;
//  PREV_functArr[2] = &RIGHT_PED;
//  PREV_functArr[3] = &RIGHT_PED_INV;

//*********waveform offset defaults*******************
  bottomval[0] = 0;  
  bottomval[1] = 0;  
  bottomval[2] = 0;    
  bottomval[3] = 0;  

  topval[0] = 255;
  topval[1] = 255;
  topval[2] = 255;
  topval[3] = 255;    


 write_pot_a (4, bottomval[0]);  //setup upper and lower voltage boundaries 
 write_pot_a (5, topval[0]);
 write_pot_b (0, bottomval[1]);
 write_pot_b (1, topval[1]);
 write_pot_b (2, bottomval[2]);
 write_pot_b (3, topval[2]); 
 write_pot_b (4, bottomval[3]);
 write_pot_b (5, topval[3]);
 
 //*********************setup wavetable interrupt timer****************************
timerLoadValue=SetupTimer2(4369);        // this is 2^16 samples divided by 15, the BMP is then shifted righht by 2 (divided by another 4) 
          
                                         // thus BPM is divided by 60 with simple math

//  table_chan_1 = &SINE_TABLE[0];
//
//  table = SQUARE_TABLE;                // default waveoutput
  
//***********************encoder pinModes and interrupt setup**********************************
pinMode(encoder0PinA, INPUT); 
  digitalWrite(encoder0PinA, HIGH);       // turn on pullup resistor
pinMode(encoder0PinB, INPUT); 
  digitalWrite(encoder0PinB, HIGH);       // turn on pullup resistor

pinMode(encoder1PinA, INPUT); 
  digitalWrite(encoder1PinA, HIGH);       // turn on pullup resistor
pinMode(encoder1PinB, INPUT); 
  digitalWrite(encoder1PinB, HIGH);       // turn on pullup resistor
 
// encoder0 pin A on interrupt 2 (pin 21)                //fix this text, had to switch the interrupts 
  attachInterrupt(3, doEncoder0A, CHANGE);
// encoder0 pin B on interrupt 3 (pin 20)
  attachInterrupt(2, doEncoder0B, CHANGE);  
  
// encoder1 pin A on interrupt 4 (pin 19)
attachInterrupt(5, doEncoder1A, CHANGE);
// encoder1 pin B on interrupt 5 (pin 18)
attachInterrupt(4, doEncoder1B, CHANGE);  

//****************************setup LCD display and pedalboard LEDs and display startup sequence**********************************
  Serial2.print("?G420");
  delay(100);	           // pause to allow LCD EEPROM to program: set up mode
  Serial2.print("?Bff");  // set backlight to 40 hex
  delay(100);              // pause to allow LCD EEPROM to program
  Serial2.print("?s6");   // set tabs to six spaces
  delay(1000);              // pause to allow LCD EEPROM to program
  Serial2.print("?D51515151515151515");  //special character for bargraph, alternating lines and spaces
  delay(100);
  Serial2.print("?c0");		                // turn cursor off
  delay(200);  
    Serial2.print("?f");                   // clear the LCD
    delay(100);
    Serial2.print("?x00?y0 Joseph Zulak 2010");
//    Serial2.print("?x00?y2     Powered by");
//    Serial2.print("?x00?y3    ARDUINO MEGA");
//  Serial2.print("?C0   Four Channel   ");  //custom boot screen
//    delay(200);	
//  Serial2.print("?C1      CV Pedal      ");  //custom boot screen
//    delay(200);	
//  Serial2.print("?C2 Joseph Zulak 2010 ");  //custom boot screen
//     delay(200);	
	
//  Serial2.print("?*");                  // display custom boot screen
    delay(1000);	

   
  DDRF = B11111111;  // sets Arduino PORT F as OUTPUT for LED's and common matrix nibble
  DDRK = B10110000;  // sets PORT K first nibble as input to read switch matrix, analog input on analog 14, work fine

  digitalWrite(66,LOW);    //analog 12   set up 0 and 5v for selector switch
  digitalWrite(67,HIGH);   //analog 13

// LED test on startup - sequentially flashes LED's on pedalboard

    Serial2.print("?x00?y2     Powered by");
    Serial2.print("?x00?y3    ARDUINO MEGA");

PORTF = led [0];
delay (150);
  
  for ( int i = 0; i < 12; i++) {  //flash 12 LED's forward
  PORTF = led[i]; 
  delay(60);                  
  }

  for ( int i = 0; i < 12; i++) {  //flash 12 LED's forward
  PORTF = led[i]; 
  delay(40);                  
  }
  
  for ( int i = 0; i < 12; i++) {  //flash 12 LED's forward
  PORTF = led[i]; 
  delay(20);                  
  }
  
    for ( int i = 0; i < 12; i++) {  //flash 12 LED's forward
  PORTF = led[i]; 
  delay(10);                  
  }
  
//  for ( int i = 11; i >= 0; i--) {  //flash 12 LED's backwards
//  PORTF = led[i]; 
//  delay(50);            
//  }
  
 delay (100);
 PORTF = B00001111; //turn off LED's after test

  Serial2.print("?y0?x00?l");
  Set_And_Restore(0,1);
  delay (50);
  Serial2.print("?y1?x00?l");
  Set_And_Restore(1,2);
  delay (50);
  Serial2.print("?y2?x00?l");
  Set_And_Restore(2,3);
  delay (50);
  Serial2.print("?y3?x00?l");
  Set_And_Restore(3,4);
  delay (2000);
  
   Serial2.print("?y0?x00?l");
   Serial2.print("1 ");
   Serial2.print("?y1?x00?l");
   Serial2.print("2 ");
   Serial2.print("?y2?x00?l");
   Serial2.print("3 ");
   Serial2.print("?y3?x00?l");
   Serial2.print("4 ");

}
